define(function(require, exports, module) {
    "require:nomunge,exports:nomunge,module:nomunge";

    var app = require('mokit/app');
    var ProjectNewView = require('../views/project-new');
    var commonUtils = require('../common/utils');
    var projectModel = require('../models/Project');

    /**
     * 社友录页面控制器
     */
    return app.controller.create({
        isSetting: true,
        /**
         * 默认action
         */
        index: function(context) {
            var self = this;
            self.setView(new ProjectNewView({
                model: projectModel
            }));
        },

        addPosition: function(context) {
            var self = this;
            var info = context.view.getNewPositionInfo(context);
            projectModel.addPosition(info, function(err) {
                if (err) {
                    self.rootView.toast(context, err);
                } else {
                    context.view.closeAddPosition(context, false, function() {
                        context.view.positionList.render();
                        context.view.clearPostionInput(context);
                    });
                }
            });
        },

        deletePosition: function(context, index) {
            var self = this;
            context.view.confirm(context, '确认删除此需求人员吗？', function(buttonIndex) {
                if (buttonIndex == 1) {
                    projectModel.deletePositionByIndex(parseInt(index));
                    context.view.render();
                }
            });
        },

        submitProject: function(context) {
            var self = this;
            var model = context.view.getNewProjectInfo(context);
            projectModel.theProject.projectName = model.projectName;
            projectModel.theProject.projectInfo = model.projectInfo;
            projectModel.submitProject(function(err) {
                if (err) {
                    self.rootView.toast(context, err);
                } else {
                    self.go(context, "/project/done", "/project/new");
                }
            });
        }
    });

});